from pms.common.views import PostFormula
from project.models import Project, Participate
from project.utils.permission import assert_project_permissions


class API(PostFormula):

    def handle(self, req):
        pid = req['id']
        assert_project_permissions(pid, self.user.id, [5])
        project = Project.objects.get(id=pid)
        delete_pid = project.id
        # 先删除所有[R1-参与]关系
        ps = Participate.objects.filter(rid__pid=project)
        for p in ps:
            p.delete()
        project.delete()
        return self.success_respond({
            'id': delete_pid
        })
